package com.yc.game.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yc.game.dao.MoneyMapper;
import com.yc.game.dao.TrainingContentMapper;
import com.yc.game.dao.TrainingMapper;
import com.yc.game.dao.TrainingVoMapper;
import com.yc.game.pojo.Training;
import com.yc.game.pojo.TrainingContent;
import com.yc.game.service.TrainingService;
import com.yc.game.vo.TrainingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainingServiceImpl implements TrainingService {
    @Autowired
    private TrainingMapper trainingMapper;
    @Autowired
    private TrainingContentMapper trainingContentMapper;
    @Autowired
    private TrainingVoMapper trainingVoMapper;

    /**
     * @param email
     * @param trainingId
     * @return
     */
    @Override
    public int getByEmailAndTrainingId(String email, Integer trainingId) {
        // 先查询是否已经预定过
        int result = trainingMapper.getByEmailAndTrainingId(email, trainingId);
        return result;
    }

    /**
     * 添加
     * @param email
     * @param trainingId
     * @return
     */
    @Override
    public int addTraining(String email, Integer trainingId) {
        Training newTraining = new Training();
        TrainingContent content = trainingContentMapper.selectByPrimaryKey(trainingId);
        newTraining.setBuyerAcct(email);
        newTraining.setStatus(1);
        newTraining.setTrainingContentId(trainingId);
        newTraining.setMoney(content.getMoney());
        newTraining.setSaleAcct(content.getSaleInfo());
        newTraining.setType(1);
        return trainingMapper.insertSelective(newTraining);
    }

    /**
     * 查询我的代练
     * @param pageNum
     * @param pageSize
     * @param email
     * @return
     */
    @Override
    public PageInfo<TrainingVo> getByEmail(Integer pageNum, Integer pageSize, String email) {
        PageHelper.startPage(pageNum, pageSize);
        List<TrainingVo> trainingVos = trainingVoMapper.getByEmail(email);
        PageInfo<TrainingVo> pageInfo = new PageInfo<>(trainingVos);
        return pageInfo;
    }
}
